/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.jm.acionista.view;

import br.jm.acionista.entity.Moeda;
import br.jm.acionista.sistema.eao.MoedaEao;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.LazyDataModel;

/**
 *
 * @author joao
 */
public class MoedaBean implements Serializable {

    private static final long serialVersionUID = 1L;
    private Moeda selectedMoeda;
    private LazyDataModel<Moeda> lazyModel;
    private FacesMessage msg = null;
    private MoedaEao moedaJpa;
    private int nPageSize = 10;

    public MoedaBean() {
        selectedMoeda = new Moeda();
        moedaJpa = MoedaEao.getInstance();
        this.lazyModel = new LazyDataModel<Moeda>() {

            public List<Moeda> load(int first, int pageSize, String sortField, boolean sortOrder, Map<String, String> filters) {
                List<Moeda> data = new ArrayList<Moeda>();

                try {
                    data = moedaJpa.getNative("Moeda", first, pageSize, sortField, sortOrder, filters, null);
                    this.setPageSize(pageSize);
                } catch (Exception ex) {
                    Logger.getLogger(MoedaBean.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                return data;
            }
        };
        this.lazyModel.setPageSize(nPageSize);
        this.lazyModel.setRowCount(moedaJpa.getCount("Moeda"));
    }

    public LazyDataModel<Moeda> getLazyModel() {
        return lazyModel;
    }

    public void setLazyModel(LazyDataModel<Moeda> lazyModel) {
        this.lazyModel = lazyModel;
    }

    public Moeda getSelectedMoeda() {
        return selectedMoeda;
    }

    public void setSelectedMoeda(Moeda selectedMoeda) {
        this.selectedMoeda = selectedMoeda;
    }

    public void inserir(ActionEvent actionEvent) {
        try {
            selectedMoeda.setIdMoeda(null);
            moedaJpa.create(selectedMoeda);

            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Inclusão efetuada com Sucesso ",
                    selectedMoeda.getNomMoeda());
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Erro ao Inserir " + selectedMoeda.getNomMoeda(),
                    e.getMessage());
        } finally {
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public void alterar(ActionEvent actionEvent) {
        try {
            moedaJpa.edit(selectedMoeda);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Alteração efetuada com Sucesso ",
                    selectedMoeda.getNomMoeda());
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Erro ao Alterar: " + selectedMoeda.getNomMoeda(),
                    e.getMessage());
        } finally {
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public void excluir(ActionEvent actionEvent) {
        try {
            moedaJpa.destroy(selectedMoeda);

            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Exclusão efetuada com Sucesso ",
                    selectedMoeda.getNomMoeda());
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Erro ao Excluir: " + selectedMoeda.getNomMoeda(),
                    e.getMessage());
        } finally {
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public void onRowSelect(SelectEvent event) {
        selectedMoeda = (Moeda) event.getObject();
    }
}
